﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using Utility;

namespace HaiFang
{
    public partial class SqlSettings : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["ImpersonateUsr"] == null || Session["ImpersonateUsrPswd"]== null)
            {
                 Response.Redirect("UserSettings.aspx"); 
            }
            if (!IsPostBack)
            {
                // 加载连接信息
                LoadSqlServerSettings();
            }
        }
        private void LoadSqlServerSettings()
        {
            string _sqlServer = String.Empty;
            string _sqlUser = String.Empty;
            string _sqlPwd = String.Empty;
            string _sqlDBName = String.Empty;


            this.txtServer.Text = ConfigSetting.GetConnectionStringField("ConnString", "server");
            this.txtSqlUser.Text = ConfigSetting.GetConnectionStringField("ConnString", "uid");
            this.txtPwdHide.Text = Security.DecodingPswd(ConfigSetting.GetConnectionStringField("ConnString", "pwd"));
            this.txtDbName.Text = ConfigSetting.GetConnectionStringField("ConnString", "database");

            this.btnSave.Visible = false;
            
        }
        private void SetConn()
        {
            this.lblMsg.Text = "";

            try
            {
                

                bool _isPass = true;
                string _conn = "server=" + this.txtServer.Text + ";uid=" + this.txtSqlUser.Text
                    + ";pwd=" + this.txtSqlPswd.Text + ";database=" + this.txtDbName.Text;
                
                try
                {
                    RunSql(_conn, "SELECT * FROM USERINFO WHERE 1<>1");
                }
                catch
                {
                    _isPass = false;
                }
                if (_isPass)
                {
                    
                    this.txtPwdHide.Text = this.txtSqlPswd.Text;
                    this.btnSave.Visible = true;
                }
                else
                {
                    this.btnSave.Visible = false;
                    this.lblMsg.Text = "无法连接资料库！";
                }
            }
            catch (Exception _ex)
            {
                this.btnSave.Visible = false;
                this.lblMsg.Text = "无法连接资料库!请检查用户名和密码是否正确!" + "<br>" + _ex.Message;
            }
        }
        protected void btnConn_Click(object sender, System.EventArgs e)
        {
            this.SetConn();
        }
        protected void btnSave_Click(object sender, System.EventArgs e)
        {
            ConfigSetting _webConfig = new ConfigSetting(Request.MapPath("./") + "web.config");
            try
            {

                // 保存数据库服务器
                string _setSqlServer = this.txtServer.Text;	//数据库服务器名称
                string _setSqlUser = this.txtSqlUser.Text;		//数据库帐号
                string _setSqlPwd = this.txtPwdHide.Text;		//数据库密码
                string _setSqlDbName = this.txtDbName.Text;		//帐套名

                string _connStr = string.Format("Uid={0};Pwd={1};Database={2};Server={3}", _setSqlUser, Security.EncodingPswd(_setSqlPwd), _setSqlDbName, _setSqlServer);
                _webConfig.SetConnectionStrings("ConnString", _connStr);
                _webConfig.SetAppSttings("Usr", Security.Encrypt(Session["ImpersonateUsr"].ToString()));
                _webConfig.SetAppSttings("Pwd", Security.Encrypt(Session["ImpersonateUsrPswd"].ToString()));
                _webConfig.SetAppSttings("Domain", Security.Encrypt(Session["ImpersonateUsrDomain"].ToString()));

                // 修改文件
                if (Session["ImpersonateUsr"] == null || Session["ImpersonateUsrPswd"]== null)
                    Response.Redirect("UserSettings.aspx");
                else
                    _webConfig.Save(Session["ImpersonateUsr"].ToString(), Session["ImpersonateUsrPswd"].ToString(), Session["ImpersonateUsrDomain"].ToString());

                // 清除Session
                Session.Clear();
                Response.Redirect("Login.aspx");
            }
            catch (Exception _ex)
            {
                this.lblMsg.Text = _ex.Message.ToString();
            }
        }

        private DataSet RunSql(string connString, string sqlString)
        {
            DataSet ds = new DataSet();
            using (SqlConnection conn = new SqlConnection(connString))
            {
                SqlDataAdapter adapter = new SqlDataAdapter(sqlString, conn);
                adapter.Fill(ds);
            }
            return ds;

        }
    }

}